##

rm(list = ls())

## 

library(tidyverse)
library(haven)
library(stargazer)

## Get soep
## Based on the code in soep_clean.do 

vlist <- c("worries_econ2",
           "redist1",
           "sex",
           "gebjahr",
           "pgisced11",
           "migback")

## Get data, subset, rename, recode NA, 

so <- haven::read_dta("data_private/soep.dta") %>% 
  dplyr::select(pid, syear, sex, gebjahr,
                plh0032, plh0033, plh0126, plh0127,
                pglfs, pgpsbil, pgisced11, pgnace, 
                migback, bula) %>% 
  dplyr::rename(worries_econ1 = plh0032,
                worries_econ2 = plh0033,
                redist1 = plh0126,
                redist2 = plh0127) %>% 
  mutate(across(all_of(vlist), ~ifelse(.<0,NA,.))) %>% 
  mutate(across(all_of(vlist), as.numeric)) %>% 
  mutate(redist2 = dplyr::recode(redist1,
                                 `2` = 1,
                                 `3` = 2,
                                 `1` = 3,
                                 `4` = NA_real_)) %>% 
  mutate(worries_econ1 = 4 - worries_econ1) %>% 
  dplyr::select(-worries_econ2, -redist2) %>% 
  filter(syear == 2005) %>% 
  filter(!is.na(redist1) & !is.na(worries_econ1)) %>% 
  mutate(age = 2005 - gebjahr) %>% 
  filter(!bula %in% -3:-1) %>% 
  mutate(bula = as.character(bula))

## Check when we have what

summary(so[, c("worries_econ1",
               "redist1",
               "sex",
               "age",
               "pgisced11",
               "migback")])

## To binary 

so <- so %>% 
  mutate(want_more_redist = ifelse(redist1 == 3, 1, 0))

## Regressions

m1 <- lm(want_more_redist ~ worries_econ1, data = so)
m2 <- lm(want_more_redist ~ worries_econ1 +
           age+sex+migback+bula, data = so)

# Table A.13: worries about the economy and support for redistribution ----

stargazer(list(m1, m2), keep.stat = c("n", "rsq"),
          keep = 'worries_econ1',
          covariate.labels = "Worries about the economy",
          add.lines = list(c("Covariates", "No", "Yes"),
                           c("State FE", "No", "Yes")),
          dep.var.labels = "DV: support for more redistribution (0/1)",
          style = "ajps")